package com.chinasolf.yygh.cmn.controller;


import com.chinasoft.model.cmn.Dict;
import com.chinasolf.result.R;
import com.chinasolf.yygh.cmn.service.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 组织架构表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2022-03-17
 */
@Api(tags="数据字典管理")
@RestController
@RequestMapping("/admin/cmn/dict")
//跨域
//@CrossOrigin

public class DictController {
    @Autowired
    private DictService dictService;


    @ApiOperation(value = "获取数据字典名称")
    @GetMapping(value = "/getName/{parentDictCode}/{value}")
    public String getName(
            @ApiParam(name = "parentDictCode", value = "上级编码", required = true)
            @PathVariable("parentDictCode") String parentDictCode,
            @ApiParam(name = "value", value = "值", required = true)
            @PathVariable("value") String value) {
        return dictService.getNameByCode(value,parentDictCode);
    }

    @ApiOperation(value = "获取数据字典名称")
    @GetMapping(value = "/getName/{value}")
    public String getName(
            @ApiParam(name = "value", value = "值", required = true)
            @PathVariable("value") String value) {
        return dictService.getNameByCode(value,"");
    }

    @ApiOperation("数据字典导入")
    @PostMapping("importData")
    public R importData(MultipartFile file){
        dictService.importDictData(file);
        return R.ok();
    }



    @ApiOperation("数据字典导出")
    @GetMapping(value = "/exportData")
    public void exportData(HttpServletResponse response){
        dictService.exportData(response);
    }

    @ApiOperation("数据字典列表")
    @GetMapping("findChildData/{id}")
    public R findChildData(@PathVariable Long id){
        List<Dict> list = dictService.getChildData(id);

        return R.ok().data("list",list);
    }

    @ApiOperation(value = "根据dictCode获取下级节点")
    @GetMapping(value = "/findByDictCode/{dictCode}")
    public R findByDictCode(
            @ApiParam(name = "dictCode", value = "节点编码", required = true)
            @PathVariable String dictCode) {
        List<Dict> list = dictService.getByDictCode(dictCode);
        return R.ok().data("list",list);
    }


}

